import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:luckdragon/db.dart';
import 'package:progress_state_button/progress_button.dart';

class settingPage extends StatefulWidget {
  settingPage({Key? key, this.title = '', this.ip = '', this.port = ''})
      : super(key: key);

  final String title;
  final String ip;
  final String port;

  @override
  _settingPage createState() => _settingPage();
}

class _settingPage extends State<settingPage> {
  ButtonState stateOnlyText = ButtonState.idle;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Container(
          alignment: Alignment.center,
          child: buildCustomButton(),
        ),
      ),
    );
  }

  Widget buildCustomButton() {
    var progressTextButton = ProgressButton(
      stateWidgets: {
        ButtonState.idle: Text(
          "Delete",
          style: TextStyle(color: Colors.white, fontWeight: FontWeight.w500),
        ),
        ButtonState.loading: Text(
          "Loading",
          style: TextStyle(color: Colors.white, fontWeight: FontWeight.w500),
        ),
        ButtonState.fail: Text(
          "Fail",
          style: TextStyle(color: Colors.white, fontWeight: FontWeight.w500),
        ),
        ButtonState.success: Text(
          "Success",
          style: TextStyle(color: Colors.white, fontWeight: FontWeight.w500),
        )
      },
      stateColors: {
        ButtonState.idle: Colors.pink,
        ButtonState.loading: Colors.blue.shade300,
        ButtonState.fail: Colors.red.shade300,
        ButtonState.success: Colors.green.shade400,
      },
      onPressed: onPressedCustomButton,
      state: stateOnlyText,
      padding: EdgeInsets.all(8.0),
    );
    return progressTextButton;
  }

  void onPressedCustomButton() {
    setState(() async {
      switch (stateOnlyText) {
        case ButtonState.idle:
          stateOnlyText = ButtonState.loading;
          try {
            List<String> servers = (await queryStrs("servers"))!;
            bool isServersRemove = servers.remove(widget.ip);
            bool isAddServers = (await addStrs("servers", servers))!;
            bool isDeleteServer = (await delete(widget.ip))!;
            if (isServersRemove && isAddServers && isDeleteServer) {
              stateOnlyText = ButtonState.success;
            } else {
              stateOnlyText = ButtonState.fail;
            }
          } catch (e) {
            stateOnlyText = ButtonState.fail;
          }
          break;
        case ButtonState.loading:
          // TODO: Handle this case.
          break;
        case ButtonState.success:
          // TODO: Handle this case.
          break;
        case ButtonState.fail:
          // TODO: Handle this case.
          break;
      }
    });
  }
}
